/**
 * 
 */
package com.nh.actionPhone;

import java.sql.Timestamp;
import java.util.List;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.nh.entity.NgExpertInterlocution;
import com.nh.entity.NgExpertRepay;
import com.nh.entity.NgInteractiveRecovery;
import com.nh.entity.NgUser;

/**
 * 项目名称：NotoginsengManage
 * 
 * 类名称：expertCallAction
 * 
 * 类描述：提交专家咨询
 * 
 * 步骤 1：在上传视频或者图片之前保存先提交问题信息记录，提交完成后服务端返回信息的id 2: 得到信息id后再带着信息id上传图片或视频信息
 * 
 * 创建人：李兴建
 * 
 * E-mail:lxj08193@163.com
 * 
 * 创建时间：2012-11-11 下午08:50:58
 * 
 * 修改人：李兴建
 * 
 * 修改时间：2012-11-11 下午08:50:58
 * 
 * 修改备注：
 * 
 * @version
 */
public class ExpertCallAction extends BasePhoneAction {
	private int startNum = 0;
	private int limit = 0;
	private String question;
	private Long optionId;
	private String repayContent;
	private NgExpertInterlocution expertInterlocution;
	private List<NgExpertRepay> expertRepayList;

	/**
	 * 发起问题
	 */
	public void saveExpertInfo() {
		NgExpertInterlocution expertInterlocution = new NgExpertInterlocution();
		expertInterlocution.setQuestion(question);
		expertInterlocution.setQuestionTime(new Timestamp(System.currentTimeMillis()));
		expertInterlocution.setState(0);
		expertInterlocution.setUserid(this.getCurrUser().getId());
		expertInterlocution.setUserDiscription("号码为：" + this.getPhone() + "用户");
		this.getBaseService().save(expertInterlocution);

		writResponse("{\"state\":\"success\",returnid:\"" + expertInterlocution.getId() + "\"}");
	}

	/**
	 * 问题列表
	 */
	public void expertInfoList() {
		Criteria criteria = this.createCriteria(NgExpertInterlocution.class);
		criteria.add(Restrictions.eq("userid", this.getCurrUser().getId()));
		criteria.addOrder(Order.asc("answerTime"));
		criteria.setFirstResult(startNum);
		criteria.setMaxResults(limit);
		// criteria.setMaxResults(6);

		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "questionTime", "answerTime", "userid", "adminId", "answer" });
		writResponse(JSONSerializer.toJSON(criteria.list(), config).toString());
	}

	/**
	 * 个人回复
	 */
	public void aswerQuestion() {
		NgExpertInterlocution expertInterlocution = this.getBaseService().findById(optionId, NgExpertInterlocution.class);
		expertInterlocution.setAnswerTime(new Timestamp(System.currentTimeMillis()));
		// String conten="";
		// if(StringUtils.isNotEmpty(expertInterlocution.getAnswer())){
		// conten=expertInterlocution.getAnswer();
		// conten=conten+ "<br/>";
		// }
		// expertInterlocution.setAnswer(conten
		// +repayContent+"【"+this.getCurrUser().getPhoneNum()+"回复】");
		expertInterlocution.setState(0);
		this.getBaseService().saveOrUpdate(expertInterlocution, NgExpertInterlocution.class);

		NgExpertRepay expertRepay = new NgExpertRepay();
		expertRepay.setRecoveryId(expertInterlocution.getId());
		expertRepay.setContent(repayContent);
		expertRepay.setRecoverTime(new Timestamp(System.currentTimeMillis()));
		expertRepay.setRecoveryType(0);
		expertRepay.setUserCode(this.getCurrUser() + "");
		expertRepay.setUserStr(this.getCurrUser().getPhoneNum());
		this.getBaseService().save(expertRepay);

		writResponse("{'state':'success'}");
	}

	/**
	 * 详情内容
	 */
	public String aswerQuestionContent() {
		expertInterlocution = this.getBaseService().findById(optionId, NgExpertInterlocution.class);
		Criteria criteria = this.createCriteria(NgExpertRepay.class);
		criteria.add(Restrictions.eq("recoveryId", optionId));
		criteria.addOrder(Order.asc("recoverTime"));
		criteria.setFirstResult(startNum);
		criteria.setMaxResults(limit);
		expertRepayList = criteria.list();
		return "aswerQuestionContent";
	}

	/**
	 * @return the question
	 */
	public String getQuestion() {
		return question;
	}

	/**
	 * @param question
	 *            the question to set
	 */
	public void setQuestion(String question) {
		this.question = question;
	}

	/**
	 * @return the startNum
	 */
	public int getStartNum() {
		return startNum;
	}

	/**
	 * @param startNum
	 *            the startNum to set
	 */
	public void setStartNum(int startNum) {
		this.startNum = startNum;
	}

	/**
	 * @return the limit
	 */
	public int getLimit() {
		return limit;
	}

	/**
	 * @param limit
	 *            the limit to set
	 */
	public void setLimit(int limit) {
		this.limit = limit;
	}

	/**
	 * @return the optionId
	 */
	public Long getOptionId() {
		return optionId;
	}

	/**
	 * @param optionId
	 *            the optionId to set
	 */
	public void setOptionId(Long optionId) {
		this.optionId = optionId;
	}

	/**
	 * @return the repayContent
	 */
	public String getRepayContent() {
		return repayContent;
	}

	/**
	 * @param repayContent
	 *            the repayContent to set
	 */
	public void setRepayContent(String repayContent) {
		this.repayContent = repayContent;
	}

	/**
	 * @return the expertInterlocution
	 */
	public NgExpertInterlocution getExpertInterlocution() {
		return expertInterlocution;
	}

	/**
	 * @param expertInterlocution
	 *            the expertInterlocution to set
	 */
	public void setExpertInterlocution(NgExpertInterlocution expertInterlocution) {
		this.expertInterlocution = expertInterlocution;
	}

	/**
	 * @return the expertRepayList
	 */
	public List<NgExpertRepay> getExpertRepayList() {
		return expertRepayList;
	}

	/**
	 * @param expertRepayList
	 *            the expertRepayList to set
	 */
	public void setExpertRepayList(List<NgExpertRepay> expertRepayList) {
		this.expertRepayList = expertRepayList;
	}

}
